package api

import (
	"github.com/kataras/iris/context"
	"server/bitkop/app/common"
	"server/bitkop/app/constants"
	"global/function"
	"server/bitkop/core"
	"strings"
	"server/bitkop/app/service"
)

var allowedType = map[string]bool{
	"both": true,
	"buy": true,
	"sale": true,
}

func TrustList(ctx context.Context) {

	trustType := ctx.URLParam("type")
	coinPair := ctx.URLParam("coin_pair")

	if trustType == "" {
		trustType = "both"
	}

	// 参数错误
	if _, ok := allowedType[trustType]; !ok || coinPair == "" {
		ctx.JSON(common.Error(constants.PARAMETER_ERROR))
		return
	}

	conn := core.BK.Redis.OTEPool.Get()
	if conn == nil {
		return
	}
	defer conn.Close()

	coinPair = strings.TrimSpace(coinPair)

	results := make(map[string]interface{})

	if trustType == "buy" {
		results["buy"] = service.GetTrustList(conn, "buy", function.GetDepthKey(coinPair, "buy", 8), 8)
	} else if trustType == "sale" {
		results["sale"] = service.GetTrustList(conn, "sale", function.GetDepthKey(coinPair, "sale", 8), 8)
	} else {
		results["buy"] = service.GetTrustList(conn, "buy", function.GetDepthKey(coinPair, "buy", 8), 8)
		results["sale"] = service.GetTrustList(conn, "sale", function.GetDepthKey(coinPair, "sale", 8), 8)
	}

	ctx.JSON(common.Success(results))
}
